package com.api.gbhg.lbj.action;

import com.api.gbhg.lbj.dao.GeneralProjectDao;
import com.api.gbhg.lbj.tool.Tools;
import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.utils.DateUtil;
import com.sap.db.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 项目导入 同步经费入库
 * @date 2021/3/14 10:30
 */
public class ImportFundStorage implements Action {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    @Override
    public String execute(RequestInfo requestInfo) {
        logger.info("进入导入项目经费入库aciton");
        String requestid = requestInfo.getRequestid();
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        DetailTable[] detailTables = CommonDao.getDetailTables(requestInfo);
        GeneralProjectDao gen=new GeneralProjectDao();
        PropertyManagementCostDao p=new PropertyManagementCostDao();
        RecordSet rs = gen.getimportFundstorageById(requestid, billTableName, detailTables[1].getTableDBName());
        Map<String,String> map=new HashMap<>();
        RecordSet recordSet = gen.deleteWorkflowByid(requestid);
        recordSet.next();
        boolean resV=false;
        while (rs.next()) {
            map.put("requestId", rs.getStringNoTrim("lcjlsqjlid"));
            map.put("lcbj", rs.getStringNoTrim("lcbj"));
            map.put("lcjlid", rs.getStringNoTrim("lcjlsqjlid"));
            map.put("nd", rs.getStringNoTrim("sheet4_detail2_field1"));
            //yszje
            String yszje = rs.getStringNoTrim("sheet4_detail2_field3");
            if (StringUtils.isBlank(yszje)) {
                yszje = "0.00";
            }
            map.put("ze", yszje);
            map.put("bmzcjjfl", Tools.getuf_jjzcflkmkKmc(rs.getStringNoTrim("sheet4_detail2_field2")));
            String dyzfjjzcfl = rs.getStringNoTrim("sheet4_detail2_field7");
            if (StringUtils.isNotBlank(dyzfjjzcfl)){
                dyzfjjzcfl=Tools.getuf_jjzcflkmkKmc(dyzfjjzcfl);
            }else {
                dyzfjjzcfl="";
            }
            map.put("zfjjfl",dyzfjjzcfl);
            map.put("syed", yszje);
            map.put("dj", "0.00");
            map.put("sbdw", rs.getStringNoTrim("sbdw"));
            map.put("xmmc", rs.getStringNoTrim("xmmin"));
            //添加归口单位、使用单位
            map.put("gkdw", rs.getStringNoTrim("gkdw"));
            map.put("sydw", rs.getStringNoTrim("sydw"));
            map.put("modedatamodifier", rs.getStringNoTrim("jbr"));
            map.put("modedatamodifydatetime", DateUtil.getNowDate());
            //判断是否已有重复数据
            resV = p.insertBudgeLines(map);
            if (!resV) {
                logger.info("============================经费额度同步失败,数据同步异常======================================");
                requestInfo.getRequestManager().setMessagecontent("同步经费额度数据异常");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }
        }
            logger.info("============================经费额度同步OK======================================");
            return SUCCESS;
        }
    }

